package com.huawei.lynnbit.tool.cocos.config.game.task;

import com.huawei.lynnbit.tool.cocos.config.game.GameConfig;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

@Slf4j
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReplaceStringTask implements Task {
    private Stage stage;
    private String type = Task.TYPE_REPLACE_STRING;
    private String srcPath;
    private String srcPattern;
    private String destString;

    @Override
    public String getTaskInfo() {
        return "run replace string in " + srcPath + " from srcPattern: " + srcPattern + " to destString: " + destString;
    }

    @Override
    public boolean isValid() {
        if (Objects.isNull(stage)) {
            log.error("type:{}, stage cannot be blank", type);
            return false;
        }

        if (StringUtils.isBlank(srcPath)) {
            log.error("type:{}, srcPath cannot be blank", type);
            return false;
        }
        if (StringUtils.isBlank(srcPattern)) {
            log.error("type:{}, srcPattern cannot be blank", type);
            return false;
        }

        if (StringUtils.isBlank(destString)) {
            log.error("type:{}, destString cannot be blank", type);
            return false;
        }

        return true;
    }

    public void presetDestPath(GameConfig gameConfig) {
        if (srcPath.contains("${devecoProject}")) {
            srcPath = srcPath.replace("${devecoProject}", gameConfig.getDevEcoProjectPath());
        }

        if (srcPath.contains("${cocosProject}")) {
            srcPath = srcPath.replace("${cocosProject}", gameConfig.getProjectPath());
        }
    }
}
